const username = document.querySelector('.username');
const password = document.querySelector('.password');
const btnLoading = document.querySelector('.btn-loading');
const submitBtn = document.querySelector('.submit-btn');
const usernameError = document.querySelector('.username-error');
const passwordError = document.querySelector('.password-error');
let curIndex = 0;
// 时间间隔(单位毫秒)，每30秒钟显示一张，数组共有8张图片放在bg文件夹下。
const timeInterval = 30000;
const imgSrc = '/images/bg/';

window.onload = function() {
	// 背景图片
	randompic();

	if (usernameError.innerHTML !== '') {
		username.className = 'username username-input-error';
		usernameError.className = 'username-error opacity1';
	}
	var qrlink = document.getElementById('op_wiseapp_qrlink');
	var qrbox = document.getElementById('op_wiseapp_qrbox');
	qrlink.onmouseover = function(event) {
		qrbox.className = 'op_wiseapp_qrboxshow';
	}
	qrlink.onmouseout = function(event) {
		qrbox.className = '';
	}
};

function handleSubmit(e) {
	if (username.value === '') {
		username.className = 'username username-input-error';
		usernameError.className = 'username-error opacity1';
		usernameError.innerHTML = '请输入用户名';
		return false;
	}
	if (password.value === '') {
		password.className = 'password password-input-error';
		passwordError.className = 'password-error opacity1';
		usernameError.innerHTML = '请输入密码';
		return false
	}
	btnLoading.style.display = 'block';
	submitBtn.style.backgroundColor = '#b3b3b3';
	submitBtn.style.cursor = 'default';
	return true;
}

function usernameInputFocus() {
	if (username.className === 'username username-input-error') {
		username.className = 'username username-input-default';
		usernameError.className = 'username-error opacity0';
	}
}

function passwordInputFocus() {
	if (password.className === 'password password-input-error') {
		password.className = 'password password-input-default';
		passwordError.className = 'password-error opacity0';
	}
}

function parseUrl(url){
	  var result = {};
	  var query = url.split("?")[1];
	  var queryArr = query.split("&");
	  queryArr.forEach(function(item){
	    var key = item.split("=")[0];
	    var value = item.split("=")[1];
	    result[key] = value;
	  });
	  return result;
	}


function randompic() {
	let url = document.getElementById("loginjs").src;
	let params = parseUrl(url);
	let param2 = params['param2']
	if(param2=='true'){
		let contextPath = params['param']
		let i = Math.floor(Math.random() * 22);// 随机选取照片
		if (curIndex == i) {
			i = Math.floor(Math.random() * 22);// 随机选取照片
		}
		curIndex = i;
		// document.body.background=imgSrc+images[curIndex];
		document.body.style.backgroundImage = "URL(" + contextPath + imgSrc + curIndex + ".jpg)";
		setTimeout("randompic()", timeInterval);
	}
}
